/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */

var isBalanced = function (root) {
    if (!root) {
        return true
    }
    // 判断左右子树是否为平衡二叉树
    if (!isBalanced(root.left) || !isBalanced(root.right)) {
        return false
    }
    // 判断左右子树的深度是否超过1
    if (Math.abs(fn(root.left) - fn(root.right)) > 1) {
        return false
    }
    return true
    function fn(root) {
        return !root ? 0 : Math.max(fn(root.left), fn(root.right)) + 1
    }
};